package Two;

import java.io.*;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class FileMerge {
    /*
    两个文件中，有相同的员工信息，有不同的员工信息。要求将两个文件进行合并，去除重复的员工信息，将新的员工信息，按工资降序排列写入新的文件c.txt。
     */

    public static Set<Person> set = new TreeSet<>(new Comparator<Person>() {
        @Override
        public int compare(Person o1, Person o2) {
            return Integer.parseInt(o2.getCode())-Integer.parseInt(o1.getCode());
        }
    });


    public static void main(String[] args) {
        InputStream in = null;
        InputStream in1 = null;
        OutputStream out = null;

        Writer w = null;


        try {
            in = new FileInputStream("Housework0915/src/Two/1.txt");
            in1 = new FileInputStream("Housework0915/src/Two/2.txt");
            out = new FileOutputStream("Housework0915/src/Two/c.txt");
            int len = 0;
            int len1 = 0;
            byte[] array = new byte[1024];
            byte[] array1 = new byte[1024];


            while ((len=in.read(array))!=-1){
                out.write(array,0,len);
            }
            while ((len1=in1.read(array1))!=-1){
                out.write(array1,0,len1);
            }


            Reader r = new FileReader("Housework0915/src/Two/c.txt");
            BufferedReader br = new BufferedReader(r);
            String str = null;
            while ((str=br.readLine())!=null){
                String[] valueArray = str.split("\\s+");

                Person p = new Person(valueArray[0],valueArray[1],valueArray[2],Integer.parseInt(valueArray[3]));
                set.add(p);
            }

//            for (Person person:set){
//                System.out.println(person);
//                System.out.println(person.getCode()+"  "+person.getName()+"  "+person.getData()+"  "+person.getSalary()+"\n");
//            }


            w = new FileWriter("Housework0915/src/Two/c.txt");
            for (Person person:set){
                w.write(person.getCode()+"  "+person.getName()+"  "+person.getData()+"  "+person.getSalary()+"\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                w.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
